/*
 * Scheduler.java
 *
 * Created on September 16, 2007, 3:12 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package simulationengine.event;

import java.util.PriorityQueue;

/**
 * Mastermind behind the simulationengine. The scheduler enforces deadlines and inserts and removes entities through event times
 * @author edrucker
 */
public class Scheduler
{
    
	private static PriorityQueue<Event> eventList = new PriorityQueue<Event> (); 
	
	private double systemTime;
    
	/** Creates a new instance of Scheduler */
	public Scheduler()
	{
		systemTime = 0.0;
	}
	
	
    /**
     * Arrange for Event to be called in the future
     * @param event Event to be called in the future
     */
	public void schedule(Event event)
	{
		eventList.add(event);
	}
	
	
    /**
     * Clear or reset scheduled event
     */
	public void cancel()
	{
		
	}
	
	
    /**
     * After current event, call next event
     */
	public void handle()
	{

		Event nextEvent = eventList.poll();
		systemTime = nextEvent.getTime();
		nextEvent.occur();
	}
	
    /**
     * Public access to system time variable.
     * @return double containing current time in the system
     */
	public double getSystemTime()
	{
		return systemTime;
	}
}
